package com.huiquan.mark_sentence.dao;

import com.huiquan.analysis.domain.AnalysisSentence;
import com.huiquan.analysis.domain.AnalysisVocabulary;
import com.huiquan.framework.base.BaseDao;
import com.huiquan.mark_sentence.domain.MarkGroup;
import com.huiquan.mark_sentence.domain.MarkSentence;
import com.huiquan.mark_sentence.domain.MarkSentenceType;
import com.huiquan.mark_sentence.domain.MarkVocabulary;
import com.ibatis.sqlmap.client.SqlMapClient;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lichao email:lichao@witspring.com
 * @Description: 语句标记 dao
 * @Date 2018/4/25 11:13
 * @since Ver V0.0.1
 */
@Repository
public class MarkSentenceDao extends BaseDao {
    @Resource(name = "sqlMapClient")
    private SqlMapClient sqlMapClient;

    public static final String space = "MARK_SENTENCE.";


    /**
     * 检索得到语句列表
     *
     * @param params
     * @return
     */
    public List<MarkSentence> selectMarkSentenceList(Map<String, Object> params) {
        return (List<MarkSentence>) this.list(space + "SELECT_MARK_SENTENCE_LIST", params, sqlMapClient);
    }

    /**
     * 检索得到语句数量
     *
     * @param params
     * @return
     */
    public int selectMarkSentenceCount(Map<String, Object> params) {
        return (int) this.object(space + "SELECT_MARK_SENTENCE_COUNT", params, sqlMapClient);
    }

    /**
     * 检索得到语句类型列表
     *
     * @return
     */
    public List<MarkSentenceType> selectMarkSentenceTypeList() {
        return (List<MarkSentenceType>) this.list(space + "SELECT_MARK_SENTENCE_TYPE_LIST", sqlMapClient);
    }

    /**
     * 更新语句type,并把状态改为已标记
     *
     * @param id
     * @param type
     */
    public void updateMarkSentenceType(long id, String type) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("type", type);
        this.update(space + "UPDATE_MARK_SENTENCE_TYPE", params, sqlMapClient);
    }

    /**
     * 更新语句type,并把状态改为已标记
     *
     * @param id
     * @param sid
     */
    public void updateMarkSentenceSid(long id, long sid) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("sid", sid);
        this.update(space + "UPDATE_MARK_SENTENCE_SID", params, sqlMapClient);
    }


    /**
     * 根据id取得MarkSentence
     *
     * @param id
     * @return
     */
    public MarkSentence selectMarkSentenceById(long id) {
        return (MarkSentence) this.object(space + "SELECT_MARK_SENTENCE_BY_ID", id, sqlMapClient);
    }

    /**
     * 插入analysisSentence并返回主键
     *
     * @param analysisSentence
     * @return
     */
    public Long insertToAnalysisSentence(AnalysisSentence analysisSentence) {
        return (Long) this.insert(space + "INSERT_TO_ANALYSIS_SENTENCE", analysisSentence, sqlMapClient);
    }

    public void insertToAnalysisSentenceEnlargeXbs(long sid, String secondType) {
        Map<String, Object> params = new HashMap<>();
        params.put("sid", sid);
        params.put("secondType", secondType);
        this.insert(space + "INSERT_TO_ANALYSIS_SENTENCE_ENLARGE_XBS", params, sqlMapClient);
    }

    public void updateAnalysisSentenceEnlargeXbs(long sid, String secondType) {
        Map<String, Object> params = new HashMap<>();
        params.put("sid", sid);
        params.put("secondType", secondType);
        this.update(space + "UPDATE_ANALYSIS_SENTENCE_ENLARGE_XBS", params, sqlMapClient);
    }

    /**
     * 得到导出的语句列表
     *
     * @return
     */
    public List<Map<String,String>> selectMarkSentenceListForExport() {
        return (List<Map<String,String>>) this.list(space + "SELECT_MARK_SENTENCE_LIST_FOR_EXPORT", sqlMapClient);
    }

    /**
     * 删除未标记的语句
     */
    @Transactional(rollbackFor=RuntimeException.class)
    public void deleteUnmarkedSentence() {
        this.delete(space + "DELETE_UNMARKED_SENTENCE", sqlMapClient);
    }

    /**
     * 关联临时表的数据插入到未标记语句中
     */
    public void insertUnmarkedSentenceWithTemp() {
        this.insert(space + "INSERT_UNMARKED_SENTENCE_WITH_TEMP", sqlMapClient);
    }

    public void initMarkVocab() {
        this.update(space + "INIT_MARK_VOCAB", sqlMapClient);

    }

    public List<MarkVocabulary> retriveVocabListByGroup(Long groupId) {
        return (List<MarkVocabulary>) this.list(space + "RETRIVE_VOCAB_LIST_BY_SID",groupId, sqlMapClient);
    }

    public List<MarkGroup> retriveGroupListBySid(Long sid) {
        return (List<MarkGroup>) this.list(space + "RETRIVE_GROUP_LIST_BY_SID",sid, sqlMapClient);
    }

    public Long insertGroup(String type, Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);
        params.put("sid", id);
        return (Long)this.insert(space + "INSERT_MARK_GROUP",params, sqlMapClient);
    }


    public Long batchInsertVocab(List<MarkVocabulary> list) {
        Map<String, Object> params = new HashMap<>();
        params.put("list", list);
        return (Long)this.insert(space + "BATCH_INSERT_VOCABULARY",params, sqlMapClient);
    }

    public void deleteGroupVocabBySid(Long id) {
        this.delete(space + "DELETE_GROUP_VOCAB_BY_SID",id, sqlMapClient);
    }

    public void updateChildIds(Long id, String childIds) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("childIds",childIds);
        this.update(space + "UPDATE_CHILD_IDS",params, sqlMapClient);
    }
    public List<AnalysisVocabulary> retrieveUnbelVocabs() {
        return (List<AnalysisVocabulary>) this.list(space + "RETRIEVE_UNLABEL_VOCABULARYS", sqlMapClient);
    }

    /**
     * 删除或者恢复
     * @param delFlag
     * @param flag
     */
    public void updateDelFlagAndFlag(Integer delFlag, Integer flag , Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("delFlag", delFlag);
        params.put("flag", flag);
        params.put("id",id);
        this.update(space + "UPDATE_DEL_FLAG_FLAG", params, sqlMapClient);

    }
}
